﻿using System;
using System.Data;
using System.Data.SqlClient;
using TeachingManageSystem.DAL;
using TeachingManageSystem.Entity;

namespace TeachingManageSystem.BLL
{
    public  class PictureBll:DBHelper
    {
        #region 存储图片
        /// <summary>
        /// 将图片转换成二进制插入到数据库中
        /// </summary>
        /// <param name="picture"></param>
        /// <returns></returns>
        public static int InsertBookImage(Picture picture)
        {
            var getpars = new SqlParameter[6];
            getpars[0] = new SqlParameter("@pictureName", picture.PicturenName);
            getpars[1] = new SqlParameter("@pictureContent", picture.Picturn);//文件内容插入This.Fileupload1.FileBytes同样可以直接转换成Byte数组不用转换
            getpars[2] = new SqlParameter("@picturePicturePath", picture.PicturePath);//保存文件类型
            getpars[3] = new SqlParameter("@picturemarkSize", picture.PictureSize);//文件长度
            getpars[4] = new SqlParameter("@pictureTime", picture.PictureMarkTime);
            getpars[5] = new SqlParameter("@pictureType", picture.PictureType);
            var sql = "Insert Into Picture(PicturenName,Picturn,PicturePath,markSize,markType,markDate) values(@pictureName,@pictureContent,@picturePicturePath,@picturemarkSize,@pictureType,@pictureTime)";
            return ExecuteSql(sql, getpars);
        }

        #endregion

        #region 从数据库中读取图片
        /// <summary>
        /// 根据图片的ID从数据库中读出图片 
        /// </summary>
        /// <param name="picture"></param>
        /// <returns></returns>
        public static SqlDataReader GetPictureByID(Picture picture)
        {
            var sqlParameters = new SqlParameter[1];
            sqlParameters[0] = MakeInParam("@id", SqlDbType.NVarChar, 50, picture.ID);
            return  GetDataReader("SelectPictureByID", sqlParameters);
        }
        #endregion
    }
}
